<?php
class Base extends MY_Admin_Controller
{
    /**
     * 代理列表
     */
    public function index()
    {
        $page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$where = array('type'=>1);
    	
    	$this->load->model('staff/Model_staff','staff');
    	
        $this->load->model('agent/Model_agent','agent');
        $total_rows = $this->agent->count($where);
        
        //分页
        $this->load->library('pagination');
        
        $config['total_rows'] = $total_rows;
        $config['per_page'] = 20;
        $config['base_url'] = '/user/agent/base';
        
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        
        $offset = ($page-1) * $config['per_page'];
        $list = $this->agent->lists($offset,  $config['per_page'], $where);
        
        
        $this->load->model('user/Model_user','u');
        foreach ($list as &$v)
        {
        	$v['anchor_num'] = $this->u->count(array('recommend_uid'=>$v['uid']));
        	$staff_info = $this->staff->getById($v['salesman']);
        	$v['salesman'] = $staff_info['realname'];
        }

        $agent_type = array('1'=>'收费代理', '2'=>'异业联盟');
        
        $data = array(
            'infoAll'     => $list,
         	'show_page'   => $show_page,
         	'total_rows'  => $config['total_rows'],
        	'agent_type'  => $agent_type,
        );
        
        $this->load->view('admin/agent/base/index', $data);
    }
    
    /*
     * user 添加,编辑
    * */
    public function create()
    {
    	$this->load->model('finance/Model_finance_bank_conf', 'fbc');
    	$bank_item = $this->fbc->getAllLists();
    	
    	//员工列表
    	$this->load->model('staff/Model_staff','staff');
    	$staff_list = $this->staff->getAllLists();
    	
    	$this->load->view('admin/agent/base/create', array('staff_list'=>$staff_list, 'bank_item'=>$bank_item));
    }
    
    public function save()
    {
    	$uname = trim($this->input->get_post('uname'));
    	$password = trim($this->input->get_post('password'));
    	
    	$company = trim($this->input->get_post('company'));
    	$license = trim($this->input->get_post('license'));
    	$address = trim($this->input->get_post('address'));
    	$charge_person = trim($this->input->get_post('charge_person'));
    	$charge_person_tel = trim($this->input->get_post('charge_person_tel'));
    	$charge_person_email = trim($this->input->get_post('charge_person_email'));
    	$bank_account = trim($this->input->get_post('bank_account'));
    	$bank = trim($this->input->get_post('bank'));
    	$salesman = trim($this->input->get_post('salesman'));
    	$remarks = trim($this->input->get_post('remarks'));
    	
    	$city = trim($this->input->get_post('city'));
    	$plate = trim($this->input->get_post('plate'));
    	$expiry_date = trim($this->input->get_post('expiry_date'));
    	
    	$bank_id = trim($this->input->get_post('bank_id'));
    	$bank_branch = trim($this->input->get_post('bank_branch'));
    	$card_no = trim($this->input->get_post('card_no'));
    	$cardholder = trim($this->input->get_post('cardholder'));
    	
    	$this->load->model('user/Model_user', 'u');
    	
    	$r_data = $this->u->register($uname, $company, $password, $charge_person_tel, '', '',
    			USER_STATUS_NORMAL, USER_SOURCE_MOBILE, USER_TYPE_AGENT);
    	if($r_data['status'] != 200)
    	{
    		die('注册失败');
    	}
    	
    	$this->load->model('finance/Model_finance_bank_conf', 'fbc');
    	$bank_item = $this->fbc->getById($bank_id);
    	
    	$this->load->model('finance/Model_finance_bind_bank_card', 'fbbc');
    	$this->fbbc->bind
    	(
    			$r_data['data']['uid'],
    			$uname,
    			$cardholder,
    			$card_no,
    			$bank_id,
    			$bank_item['name'],
    			$bank_branch,
    			$charge_person_tel
    	);
    	
    	$data = array(
    			'type'=>1,
    			'uid'=>$r_data['data']['uid'],
    			'company'=>$company,
    			'city'=>$city,
    			'plate'=>$plate,
    			'license'=>$license,
    			'address'=>$address,
    			'charge_person'=>$charge_person,
    			'charge_person_tel'=>$charge_person_tel,
    			'charge_person_email'=>$charge_person_email,
    			'salesman'=>$salesman,
    			'remarks'=>$remarks,
    			'expiry_date'=>$expiry_date,
    			'create_time'=>date('Y-m-d H:i:s'),
    			);
    	
    	$this->load->model('agent/Model_agent','agent');
    	$agent_save = $this->agent->save($data);
    	
    	$this->load->helper('url');
    	redirect("/agent/base/index");
    }
}